package com.sitech.portal.server.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.common.form.TreeNode;
import com.sitech.ibnms.common.service.IAsyncTreeService;
import com.sitech.portal.system.dao.ISysModuleDao;
import com.sitech.portal.system.dao.ISysRoleDao;
import com.sitech.portal.system.dao.ISysSubSystemDao;
import com.sitech.portal.system.domain.TblSYS_MODULE;
import com.sitech.portal.util.CommUtil;
import com.sitech.portal.util.UrlSSOUtil;
/**
 * 
 * @author zhangdajun
 * 实现liujh提供接口
 * 实现左树展示
 */
public class ModuleAsyncTreeServiceImpl implements IAsyncTreeService{
	
	private ISysModuleDao moduleDao;
	private ISysSubSystemDao sysSubSystemDao;
    private ISysRoleDao sysRoleDao;

    public void setSysRoleDao(ISysRoleDao sysRoleDao) {
        this.sysRoleDao = sysRoleDao;
    }

    public ISysSubSystemDao getSysSubSystemDao() {
		return sysSubSystemDao;
	}

	public void setSysSubSystemDao(ISysSubSystemDao sysSubSystemDao) {
		this.sysSubSystemDao = sysSubSystemDao;
	}

	public ISysModuleDao getModuleDao() {
		return moduleDao;
	}

	public void setModuleDao(ISysModuleDao moduleDao) {
		this.moduleDao = moduleDao;
	}

	@Override
	public TreeNode getRootNode(String userId, String rootId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TreeNode> getSubNodes(String loginId,TreeNode pnode) throws Exception{
		
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		
		String pid = pnode.getId();
		if(pid!=null){
		List<HashMap> moduleResults =null;
        Boolean isAdmin =   sysRoleDao.isAdmin(loginId);
        if(isAdmin||loginId.equals(CommUtil.USER_ADMIN) || pnode.isNocheck()){	//不判断权限
			moduleResults = moduleDao.findModuleListByParentId(pid);
		}else{
			moduleResults = moduleDao.findModuleListByLoginIDAndParentId(loginId,pid);
		}
		//将hashmap分装成treenode对象
		for(HashMap map : moduleResults){
			String path = ""+map.get("MODULE_PATH");
			if(map.get("MODULE_PATH")!=null && "1".equals(map.get("SSO_TYPE")))
				path = UrlSSOUtil.encryptUrl(map.get("MODULE_PATH").toString(), loginId);
			
			TreeNode treeNode = new TreeNode();
			treeNode.setpId(""+map.get("PARENT_ID"));
			treeNode.setId(""+map.get("MODULE_ID"));
			treeNode.setName(""+map.get("MODULE_NAME"));
			treeNode.setUserData("level", ""+map.get("MODULE_LEVEL"));
			treeNode.setUserData("PATH", path);
			treeNode.setUserData("IS_REFRESH", ""+map.get("IS_REFRESH"));
			treeNode.setUserData("TARGET", ""+map.get("MODULE_TARGET"));
            treeNode.setUserData("ENABLE", ""+map.get("MODULE_ENABLE"));
			if("0".equals(map.get("CHILDREN").toString()))
				treeNode.setIsParent(false);
			treeNodes.add(treeNode);
		}
		}
		
		return treeNodes;
	}

}
